// Saves options to localStorage.
function save_options() {
	globalSettings.setSetting('close_timer', Number($('#close_timer').val()) * 1000);
	globalSettings.setSetting('process_limit', Number($('#limit').val()));
	globalSettings.setSetting('refresh_timer', Number($('#refresh_timer').val()));
	globalSettings.setSetting('process_direction', $('#radio_direction :radio:checked').attr('id') == 'dir1' ? 0 : 1);
	globalSettings.setSetting('process_order', $('#allow_process_order').is(':checked') ? true : false);
	globalSettings.setSetting('process_order_array', $('#process_order').sortable('toArray').toString());
	globalSettings.setSetting('close_others', $('#close_all_notes').is(':checked') ? true : false);
	globalSettings.setSetting('gather_only', $('#only_gather_notes').is(':checked') ? true : false);
	globalSettings.setSetting('process_time', $('#process_timepkr').val());
	
	// Update status to let user know options were saved.
	var status = document.getElementById('status');
	status.innerHTML = "Options Saved.";
	setTimeout(function() {
		status.innerHTML = "";
	}, 2500);
}

function reorder(order, container)  {
console.log(order);
	var arr = order.split(',');
	$.each(arr, function(key, val){
	  container.append($('#'+val));
	});
}

$(document).ready(function() {
	$(document).height = '250px';
	
	tooltip();
	
	$('#radio_direction').buttonset();
	
	$('#close').bind('click', function() {
		window.close();
	});
	
	$('#close_timer').spinner({ max: 30, min: 1 });
	$('#limit').spinner({max: 999, min: 0, step: 5});
	$('#refresh_timer').spinner({min:1, max:1440});
	
	$('#allow_process_order').bind('click', function() {
		if ($(this).is(':checked')) {
			$('#process_order').sortable('enable');
		} else {
			$('#process_order').sortable('disable');
		}
	});
	
	$('#process_order').sortable({
		placeholder: 'drop-highlight  ui-corner-all'
	});
	$('#process_order').disableSelection(true);
	
	$('#only_gather_notes').bind('click', function() {
		if ($(this).is(':checked')) {
			$('#process_time').show();
		} else {
			$('#process_time').hide();
		}
	});
	
	$('#process_timepkr').timepicker({
		showPeriod: true,
		showLeadingZero: true,
		defaultTime: '12:00',
		showOn: 'focus'
	});
	
	document.querySelector('#save').addEventListener('click', save_options);
	globalSettings.getAll(function(setting) { 
		$('#close_timer').val((Number(setting.close_timer) / 1000));
		$('#limit').val(Number(setting.process_limit));
		$('#refresh_timer').val(Number(setting.refresh_timer));
		if (Number(setting.process_direction) == 0) {
			$('#dir1').click();
		} else {
			$('#dir2').click();
		}
		
		reorder(setting.process_order_array, $('#process_order'));
		
		if (setting.process_order == true) {
			$('#allow_process_order').click();
			$('#process_order').sortable('enable');
		} else {
			$('#process_order').sortable('disable');
		}
		
		if (setting.close_others == true) {
			$('#close_all_notes').click();
		}
		
		$('#process_timepkr').val(setting.process_time);
	
		if (setting.gather_only == true) {
			$('#only_gather_notes').click();
			$('#process_time').show();
		} else {
			$('#process_time').hide();
		}
	});
});

this.tooltip = function(){	
	xOffset = 10;
	yOffset = 20;
	tWidth = 300;
	$("div.icon.help.tooltip-left").hover(function(e){
		this.t = this.title;
		this.title = "";
		$("body").append("<p class='eom' id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("width", tWidth + "px")
			.css("position", "absolute")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");
    },
	function(){
		this.title = this.t;
		$("#tooltip").remove();
    });
	$("div.icon.help.tooltip-left").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
	
	$("div.icon.help.tooltip-right").hover(function(e){
		this.t = this.title;
		this.title = "";
		$("body").append("<p class='eom' id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("width", tWidth + "px")
			.css("position", "absolute")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX - (tWidth + (yOffset *2))) + "px")
			.fadeIn("fast");
    },
	function(){
		this.title = this.t;
		$("#tooltip").remove();
    });
	$("div.icon.help.tooltip-right").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX - (tWidth + (yOffset *2))) + "px");
	});
};